<template>
  <div class="login-box">
    <div class="login-form">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        class="demo-ruleForm"
        status-icon
      >
        <el-form-item label="" prop="name">
          <el-input placeholder="用户名：admin" v-model="ruleForm.name">
            <template #prefix>
              <el-icon class="el-input__icon"><user /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            type="password"
            placeholder="密码：123456"
            v-model="ruleForm.password"
            show-password
            autocomplete="new-password"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            登录
          </el-button>
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
import { reactive, ref, toRefs } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { login } from "../../request/api";
import { ElMessage } from "element-plus";
import router from "@/router";

export default {
  setup() {
    const data = reactive({
      ruleForm: {
        name: "",
        password: "",
      },
      rules: {
        name: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
      },
    });
    const ruleFormRef = ref<FormInstance>();
    const submitForm = (formEl: FormInstance | undefined) => {
      if (!formEl) return;
      formEl.validate((valid) => {
        if (valid) {
          console.log('1111111111', data.ruleForm)
          login(data.ruleForm).then((res) => {
            if (data.ruleForm.name == res.data.name && data.ruleForm.password == res.data.password) {
              ElMessage({
                message: "登录成功",
                type: "success",
              });
              sessionStorage.setItem("token", res.data.token);
              router.push("/home/index");
            } else {
              ElMessage({
                message: "账号或密码错误",
                type: "error",
              });
            }
          });
        } else {
          return false;
        }
      });
    };
    const resetForm = (formEl: FormInstance | undefined) => {
      if (!formEl) return;
      formEl.resetFields();
    };
    return {
      ...toRefs(data),
      ruleFormRef,
      submitForm,
      resetForm,
    };
  },
};
</script>

<style lang="scss" scoped>
.login-box {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  .login-form {
    width: 420px;
    padding: 50px 40px 45px;
    background-color: var(--el-bg-color);
    border-radius: 10px;
    box-shadow: #0000001a 0 2px 10px 2px;
    .demo-ruleForm {
      .el-form-item {
        margin-bottom: 40px;
      }
      .login-btn {
        display: flex;
        justify-content: space-between;
        width: 100%;
        margin-top: 40px;
        white-space: nowrap;
        .el-button {
          width: 185px;
          height: 40px;
          border-radius: 30px;
        }
      }
    }
  }
}
::v-deep .el-button--primary {
  background: #0da7eb;
}
</style>
